import { Component, OnInit, ViewChild } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { MyModalHelper } from '@core/my-modal.helper';
import { AgentLevelService } from '@core/routes-services/agent-level.service';
import { AgentService } from '@core/routes-services/agent.service';
import { AgentLevelChangeRecordService } from '@core/routes-services/agent-level-change-record.service';

@Component({
  selector: 'app-agent-level-change-record',
  templateUrl: './agent-level-change-record.component.html',
})
export class AgentLevelChangeRecordComponent implements OnInit {

  isLoading = false;
  keyword: any;
  childId: any;
  parentId: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  levelOptions: any [] = [];
  allAgent: any [] = [];
  constructor(private message: NzMessageService,
              private agentService:AgentService,
              private agentLevelService: AgentLevelService,
              private agentLevelChangeRecordService: AgentLevelChangeRecordService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initAllAgent();
    this.initLevelOptions();
    this.initPage();
  }
  initLevelOptions() {
    this.isLoading = true;
    this.agentLevelService.getAll().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.levelOptions = res.data.agentLevelsList;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }
  initAllAgent(){
    this.isLoading = true;
    this.agentService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.allAgent = res.data.agents;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
  }
  initPage() {
    this.isLoading = true;
    this.agentLevelChangeRecordService.pagination(this.page, this.keyword,this.childId,this.parentId).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }


}
